<?php
/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @copyright Copyright 2008 Rodrigo Rutkoski Rodrigues
 */

function sy_exception_handler($e)
{
  if (SY_DEBUG_ENABLED) {
    pre($e->getMessage());
    pre($e->getTraceAsString());
    pre($e->getFile() . ' - ' . $e->getLine());
    
    sy_log('exception', $e->getMessage());
  }
}

function sy_error_handler($errno, $errstr, $errfile, $errline)
{
  $type = array(
    1 => 'E_ERROR', 2 => 'E_WARNING', 4 => 'E_PARSE', 8 => 'E_NOTICE', 16 => 'E_CORE_ERROR', 32 => 'E_CORE_WARNING', 64 => 'E_COMPILE_ERROR', 128 => 'E_COMPILE_WARNING', 
    256 => 'E_USER_ERROR', 512 => 'E_USER_WARNING', 1024 => 'E_USER_NOTICE', 2048 => 'E_STRICT', 4096 => 'E_RECOVERABLE_ERROR', 8192 => 'E_DEPRECATED', 16384 => 'E_USER_DEPRECATED', 
    30719 => 'E_ALL' 
  );

  /*$errtype = $type[$errno];
  $backtrace = debug_backtrace();
  require_once (SY_DIR . '/lib/debug/sy_error_page.php');*/
  //printf('%s: %s in %s on line %s', $errtype, $errstr, $errfile, $errline);
  
  /*echo '<ul>';
  foreach (debug_backtrace() as $err) {
    extract($err);
    printf('<li>%s (%s): %s</li>', $file, $line, $function);
  }
  echo '</ul>';*/
  
  die();
}

function sy_log($file, $data) {
  $file = Simplify::getConfig()->get('app_dir') . '/logs/' . $file . '.log';
  $data = date('Y:m:d H:i:s') . ' -> ' . trim($data);
  
  $a = file($file, FILE_IGNORE_NEW_LINES);
  if (count($a) > 30) {
    $a = array_slice($a, count($a) - 30);
  }
  
  $a[] = $data;
  
  file_put_contents($file, implode("\n", $a));
}    

function fatal_error_handler($buffer) {
  /*if (Simplify::getRequest()->isAjax()) {
    sy_log('buffer', $buffer);
  }*/
  if (ereg("(error</b>:)(.+)(<br)", $buffer, $regs) && Simplify::getRequest()->isAjax()) {
    $err = preg_replace("/<.*?>/","",$regs[2]);
    sy_log('error', $err);
    return "Error caught. Check log file (1).";
  }

  return $buffer;
}

function handle_error($errno, $errstr, $errfile, $errline)
{
  $err = "$errstr in $errfile on line $errline";
  //sy_log('error', $err);
  
  //if($errno == FATAL || $errno == ERROR) {
  if ($errno == E_COMPILE_ERROR || $errno == E_ERROR) {
    ob_end_flush();
    echo "Error caught. Check log file (2).";
    exit(0);
  }
}




define_once('SY_DEBUG_ENABLED', false);

if (SY_DEBUG_ENABLED) {
  define_once('E_DEPRECATED', 8192);
  error_reporting(E_ALL & ~ E_DEPRECATED);
  ini_set('display_errors', 1);

  ob_start("fatal_error_handler");
  set_error_handler("handle_error");
  
  set_exception_handler('sy_exception_handler');
}
else {
  error_reporting(E_NONE);
  ini_set('display_errors', 0);
}

?>